<?php

namespace app\achievement\controller;

use app\common\model\MenuModel;

/**
 *
 * @author yq
 * 系统菜单栏管理
 */
class Menu extends Base {

    /**
     * 系统菜单栏展示
     */
    public function index() {
        $menu = new MenuModel();
        $admin_rule = $menu->getAllMenu($this->set['auth_rule']);
        $arr = $menu->rule($admin_rule);
        $this->assign('admin_rule', $arr);
        return $this->fetch();
    }

    /**
     * 菜单栏修改状态
     */
    public function edit() {
        if (request()->isPost()) {
            $id = input("param.id");
            //实例化系统菜单栏model
            $menu = new MenuModel();
            $where = ['id' => $id];
            $db_name = $this->set['auth_rule'];
            //查询菜单栏数据
            $menuOne = $menu->getOneMenu($where, '', $db_name);
            if ($menuOne['status'] == 1) {
                $update = $menu->modify($where, ['status' => 0], $db_name);
                return json(['code' => 1, 'data' => '', 'msg' => '已禁止']);
            } else if ($menuOne['status'] == 0) {
                $update = $menu->modify($where, ['status' => 1], $db_name);
                return json(['code' => 1, 'data' => '', 'msg' => '已开启']);
            }
        }
    }

    /**
     * 菜单栏修改菜单显示状态
     */
    public function menutitle() {
        if (request()->isPost()) {
            $id = input("param.id");
            //实例化系统菜单栏model
            $menu = new MenuModel();
            $where = ['id' => $id];
            $db_name = $this->set['auth_rule'];
            //查询菜单栏数据
            $menuOne = $menu->getOneMenu($where, '', $db_name);
            if ($menuOne['menutitle'] == 1) {
                $update = $menu->modify($where, ['menutitle' => 0], $db_name);
                return json(['code' => 1, 'data' => '', 'msg' => '已禁止']);
            } else if ($menuOne['menutitle'] == 0) {
                $update = $menu->modify($where, ['menutitle' => 1], $db_name);
                return json(['code' => 1, 'data' => '', 'msg' => '已开启']);
            }
        }
    }

    /**
     *
     *  修改菜单栏
     */
    public function ModifyTheSort() {
        if (request()->isPost()) {
            $param = $this->request->post();
            $menu = new MenuModel();
            if (!empty($param['url'])) {
                $param['url'] = UrlProcessing($param['url']);
            }

            $where = ['id' => $param['id']];
            unset($param['id']);
            $db_name = $this->set['auth_rule'];
            $update = $menu->modify($where, $param, $db_name);

            if ($update) {
                return json(['code' => 1, 'msg' => '修改成功', 'data' => '']);
            } else {
                return json(['code' => 2, 'msg' => '修改失败', 'data' => '']);
            }
        } else {
            $id = input("param.id");
            $menu = new MenuModel();
            $where = ['id' => $id];
            $db_name = $this->set['auth_rule'];
            //查询菜单栏数据
            $menuOne = $menu->getOneMenu($where, '*', $db_name);
            //获取全部菜单
            $menus = $menu->getAllMenu($db_name, ['status' => 1, 'menutitle' => 1]);
            $menusNode = $menu->rule($menus);
            $this->assign('data', $menuOne);
            $this->assign('node', $menusNode);
            return $this->fetch();
            //return $this->fetch('menu/modifythesort');
        }
    }

    /**
     *  添加菜单栏
     */
    public function addMenu() {
        if (request()->isPost()) {
            $data = $this->request->post();

            $menu = new MenuModel();
            //组装入库数据
            $param = $data['sendData'];
            $param['url'] = urldecode($param['url']);
            if (empty($param['status'])) {
                $param['status'] = 0;
            }
            $param['url'] = UrlProcessing($param['url']);
            $add = $menu->AddMenu($this->set['auth_rule'], $param);
            if ($add) {
                return json(['code' => 1, 'msg' => '添加成功', 'data' => url('Authgroup/index') . '?type=3']);
            } else {
                return json(['code' => 2, 'msg' => '添加失败', 'data' => '']);
            }
        }
    }

    /**
     * 删除系统菜单栏
     */
    public function delMenu() {
        if (request()->isPost()) {
            $param = $this->request->post();
            $menu = new MenuModel();
            $data = $menu->getOneMenu(['pid' => $param['id']], '', $this->set['auth_rule']);
            if ($data) {
                return json(['code' => 2, 'msg' => '当前菜单下存在子级菜单，删除失败', 'data' => '']);
            }
            $del = $menu->del(['id' => $param['id']], $this->set['auth_rule']);
            if ($del) {
                return json(['code' => 1, 'msg' => '删除成功', 'data' => '']);
            } else {
                return json(['code' => 2, 'msg' => '删除失败', 'data' => '']);
            }
        }
    }

}
